﻿

using XT.Common.Extensions;
using HbcServerProcess.Interfaces;
using IotContract.Interfaces.Process;
using IotContract.Models.Process;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IotContract.Enums;

namespace HbcServerProcess;
/// <summary>
/// Open数据提供
/// </summary>
public class ProcessOpenProvider:IProcessOpenProvider
{
    private readonly IProcessDataService _processDataService;
    private readonly IProcessJobService _processJobService;
    public ProcessOpenProvider(IProcessDataService processDataService,IProcessJobService processJobService)
    {
        _processDataService= processDataService;
        _processJobService= processJobService;
    }

    /// <summary>
    /// 获取物流线数据
    /// </summary>
    /// <returns></returns>
    public List<ProcessBaseData> GetUnitCVs()
    {
       var datas= _processDataService.CvUnitTags.Values.ToList();
        List<ProcessBaseData> cvUnits = new List<ProcessBaseData>();
       
        foreach (var data in datas)
        {
           
            ProcessCvData cvUnit = new ProcessCvData
            {
               
                ErrorCode = data.ErrorCode,
                Dest = data.Dest,
                IsExistTray = data.IsExistTray1 ? 1 : 0,
                Active = data.Active,
                EquipId=data.EquipId,
                EquipNum = data.EquipNum.To<int>(),
                Num = data.TagNum,
                JobNumber = data.JobNumber,
                JobNumberOld = data.OriginJobNumber,
                JobType = data.JobType,
               
                ProtobufType=data.ProtobufType,
                TrayType = data.TrayType,
                TrayCode = $"{data.Tray1},{data.Tray2},{data.Tray3},{data.Tray4},{data.Tray5},{data.Tray6}",
              
                IsBcrReady = data.IsBcrReady ? 1 : 0,
                IsStackComplete = data.MagazineOut ? 1 : 0,
                Message = data.Message,
                IsConnect = data.IsRealTime(),
                Mode = data.IsManual ? 1 : 0,
                ReadAllNum = data.MaxReadBcr,
                ReadOkNum = data.ReadOkBcr,
                TrayOnTime = data.TrayOnTime,
                HasBcr=data.Bcrs?.Count() > 0,


            };
           
            cvUnits.Add(cvUnit);
        }

        return cvUnits;
    }
    /// <summary>
    /// 获取堆垛机数据
    /// </summary>
    /// <returns></returns>
    public List<ProcessBaseData> GetStackers()
    {
        var datas= _processDataService.ScUnits.Values.ToList();
        List<ProcessBaseData> scUnits = new List<ProcessBaseData>();
        
        foreach (var data in datas)
        {
            ProcessScData scUnit = new ProcessScData
            {             
                ErrorCode = data.ErrorCode,
                IsExistTray = data.IsExistTray1 ? 1 : 0,
                IsExistTray2 = data.IsExistTray2 ? 1 : 0,
                EquipId = data.EquipId,
                Num = data.EquipNum,
                JobNumber1 = data.W_JobNumber,
                State = data.SC_State,
                Mode = data.SC_Mode,
                JobState1 = data.JobEndFork1,
                JobType1 = data.JobType,
                Take_Station1 = data.FromStation,
                Take_Bank1 = data.FromBank,
                Take_Bay1 = data.FromBay,
                Take_Level1 = data.FromLevel,
                Set_Bank1 = data.ToBank,
                Set_Bay1 = data.ToBay,
                Set_Level1 = data.ToLevel,
                Set_Station1 = data.ToStation,
                IsConnect = data.IsRealTime(),
                ActionType = data.ActionType,
                JobNumber2 = data.JobNumberFork2,
                Active = data.Active,
                JobState2 = data.JobEndFork2,
                Take_Station2 = data.FromStationFork2,
                Take_Bank2 = data.FromBankFork2,
                Take_Bay2 = data.FromBayFork2,
                Take_Level2 = data.FromLevelFork2,
                Set_Bank2 = data.ToBankFork2,
                Set_Bay2 = data.ToBayFork2,
                Set_Level2 = data.ToLevelFork2,
                Set_Station2 = data.ToStationFork2,
            };
          
            scUnits.Add(scUnit);
        }

        return scUnits;

    }
    /// <summary>
    /// 1入库，2出库，3移库
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public List<ProcessBaseData> GetJobs(ProcessProviderType type)
    {
        List<ProcessBaseData> jobs = new List<ProcessBaseData>();
        if (type == ProcessProviderType.EnterJob)
        {
            var enters= _processJobService.ScEnterJobDic.Values.ToList();
            jobs.AddRange(enters);
        }
        else if (type == ProcessProviderType.OutJob)
        {
            var outers= _processJobService.ScOutJobDic.Values.ToList();
            jobs.AddRange(outers);
        }
        else
        {
            var moves= _processJobService.ScMoveJobDic.Values.ToList();
            jobs.AddRange(moves);
        }
        return jobs;      
    }

    public string AddJob(ProcessBaseData job, ProcessProviderType type)
    {
        throw new NotImplementedException();
    }

    public string RemoveJob(ProcessBaseData job, ProcessProviderType type)
    {
        throw new NotImplementedException();
    }
}
